﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Domain;
using Cms.Web.Framework.Controls;
using Cms.Web.UI.Admin.HelperClasses;
using iLib.Web.UI;

namespace Cms.Web.UI.Admin
{
    public partial class EditCategory : AdminPage, IEditCategoryView
    {
        private EditCategoryPresenter _presenter;

        #region IEditCategoryView Members

        public int CategoryID
        {
            get
            {
                if (ViewState["CategoryID"] == null)
                    ViewState["CategoryID"] = QueryString.GetInt32ID("id");
                return (int) ViewState["CategoryID"];
            }
        }


        public string Name
        {
            get { return txtName.Text; }
            set { txtName.Text = value; }
        }


        public string FolderName
        {
            get { return txtFolderName.Text; }
            set { txtFolderName.Text = value; }
        }


        public DefaultCategorySortKey SelectedDefaultSortKey
        {
            get
            {
                return
                    (DefaultCategorySortKey)
                    Enum.Parse(typeof(DefaultCategorySortKey), ddlDefaultSortKeys.SelectedValue);
            }
            set { ddlDefaultSortKeys.SelectedValue = value.ToString(); }
        }


        public void SetParentCategories(IList<Category> rootCategories)
        {
            ddlParentCategories.Items.Clear();
            ddlParentCategories.Items.Add(new ListItem(GetText(this, "NoParentCategory"), "-1"));
            CategoriesDropDownHelper.SetCategoryTree(ref ddlParentCategories, 0, rootCategories);
        }


        public int SelectedParentCategoryID
        {
            get { return int.Parse(ddlParentCategories.SelectedValue); }
            set { ddlParentCategories.SelectedValue = value.ToString(); }
        }


        public string ContextTranslationFile
        {
            get { return "Admin/Translations/EditCategory.xml"; }
        }

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _presenter = new EditCategoryPresenter(this);
            if (CategoryID > 0)
                HeadingText = GetText(this, "EditCategoryHeading");
            else
                HeadingText = GetText(this, "NewCategoryHeading");
            if (!Page.IsPostBack)
            {
                ddlDefaultSortKeys.Items.Add(new ListItem(GetText(this, "DefaultSortKeyNone"), "None"));
                ddlDefaultSortKeys.Items.Add(new ListItem(GetText(this, "DefaultSortKeyDateTime"), "DateTime"));
                _presenter.Initialize();
                HeadingIcon = litIcon.Text;
            }
            txtName.Focus();
        }


        protected void lbSave_Click(object sender, EventArgs e)
        {
            _presenter.Save();
            if (_presenter.IsValid)
                Response.Redirect("Categories.aspx");
        }
    }
}